package com.braveman.im.ui.activity;

import android.view.View;
import android.widget.EditText;

import com.braveman.im.R;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.TitleBarView;
import com.ht.baselibs.utils.UIUtils;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMGroupOptions;
import com.hyphenate.exceptions.HyphenateException;

public class NewGroupActivity extends BaseActivity {
    private EditText mEditGroupName;
    private EditText mEditGroupIntroduction;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("新建群组").addActions(new TitleBarView.Action("保存") {
            @Override
            public void performAction(View view) {
                /**
                 * 创建群组
                 * @param groupName 群组名称
                 * @param desc 群组简介
                 * @param allMembers 群组初始成员，如果只有自己传空数组即可
                 * @param reason 邀请成员加入的reason
                 * @param option 群组类型选项，可以设置群组最大用户数(默认200)及群组类型@see {@link EMGroupManager.EMGroupStyle}
                 *               option.inviteNeedConfirm表示邀请对方进群是否需要对方同意，默认是需要用户同意才能加群的。
                 *               option.extField创建群时可以为群组设定扩展字段，方便个性化订制。
                 * @return 创建好的group
                 * @throws HyphenateException
                 */

                final String groupName = mEditGroupName.getText().toString().trim();
                final String desc = mEditGroupIntroduction.getText().toString().trim();


                if (groupName != null) {

                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                String reason = "invite to join the group";
                                EMGroupOptions option = new EMGroupOptions();
                                option.maxUsers = 200;
                                option.style = EMGroupManager.EMGroupStyle.EMGroupStylePrivateMemberCanInvite;
                                String[] allmembers = {"qu123456","qu654321"};
                                EMClient.getInstance().groupManager().createGroup(groupName, desc, allmembers, reason, option);
                                UIUtils.showToast("创建成功");
                            } catch (HyphenateException e) {
                                e.printStackTrace();
                                UIUtils.showToast("创建失败" + e.getMessage());
                            }
                        }
                    });
                    thread.start();


                }
            }
        });
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_new_group;
    }

    @Override
    protected void initChildView() {
        mEditGroupName = (EditText) findViewById(R.id.edit_group_name);
        mEditGroupIntroduction = (EditText) findViewById(R.id.edit_group_introduction);
    }

    @Override
    protected void initChildData() {

    }

    @Override
    protected void setChildViewListener() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);


        switch (v.getId()) {


        }


    }

}
